package com.zhuss.product.processor;

import java.util.Date;
import java.util.List;

import com.zhuss.enums.StartStopEnum;
import com.zhuss.product.processor.cache.SiteCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.product.site.CreateSiteBo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.enums.SiteType;
import com.zhuss.exception.DzcxException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.product.db.dao.ext.SiteDao;
import com.zhuss.product.db.entity.Site;
import com.zhuss.util.JsonUtil;


/**
 * @description: 站点创建
 * @author: zhushanshuo
 * @create: Wed Dec 30 08:43:41 CST 2020
 **/
@Component("siteCreate")
public class SiteCreate extends AbstractProcessor<Object, CreateSiteBo> implements Processor<Object, CreateSiteBo> {

    @Autowired
    private SiteDao siteDao;

    @Autowired
    @Qualifier("siteRead")
    private SiteRead siteRead;

    @Autowired
    @Qualifier("siteCache")
    private SiteCache siteCache;


    /**
     * 校验
     */
    @Override
    public void check(CreateSiteBo bo) throws DzcxException {
        String name = bo.getName();

        if (name == null || "".equals(name.trim())) {
            throw new DzcxException(ExceptionCode.站点名称为空);
        }

        if (bo.getLng() == null || "".equals(bo.getLng().trim())) {
            throw new DzcxException(ExceptionCode.经度为空);
        }

        if (bo.getLat() == null || "".equals(bo.getLat().trim())) {
            throw new DzcxException(ExceptionCode.纬度为空);
        }

        List<Site> list = siteRead.list(name);
        String lng = bo.substring(bo.getLng());
        String lat = bo.substring(bo.getLat());

        for (Site site : list) {
            if ((bo.substring(site.getLng()).equals(lng)) && (bo.substring(site.getLat()).equals(lat))
                    && (site.getName().equals(name))) {
                throw new DzcxException(ExceptionCode.站点已经存在);
            }
        }
    }

    /**
     * 操作方法
     */
    @Override
    public Object process(CreateSiteBo param) {
        Site record = new Site();
        record.setType(param.getType());
        record.setName(param.getName());
        record.setAddress(param.getAddress());
        record.setLng(param.getLng());
        record.setLat(param.getLat());
        record.setDirection(param.getDirection());
        record.setInfo(param.getInfo());
        record.setStatus(param.getStatus() == null ? StartStopEnum.启用.key() : param.getStatus());
        record.setSort(param.getSort());


        record.setCreatorId(param.getConcurrentUserId());
        record.setCreator(param.getConcurrentUserName());
        record.setCreateTime(new Date());
        siteDao.create(record);
        if (param.getType() != null) {
            record.setSiteNo(SiteType.getTag(param.getType()) + record.getSiteId());
        }
        siteDao.updateByPrimaryKeySelective(record);
        param.setTargetId(record.getSiteId());
        siteCache.clear(record.getSiteId());
        return null;
    }

    /**
     * 操作日志
     */
    @Override
    public Object log(CreateSiteBo param) throws DzcxException {
        OpLog record = new OpLog();
        record.setMoudle(MoudleEnum.商品.key());
        record.setAction(LogActionEnum.站点创建.key());
        record.setTargetId(param.getTargetId());
        record.setTargetName(param.getName());
        record.setContent("参数: " + JsonUtil.toJson(param));
        record.setReason(param.getExceptionReason());

        record.setCreatorId(param.getConcurrentUserId());
        record.setCreator(param.getConcurrentUserName());
        record.setCreateTime(new Date());
        log(record);
        return null;
    }
}
